package chances.epg.commons.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public abstract class DateUtils {
	static final SimpleDateFormat DateFormatter = new SimpleDateFormat("yyyy-MM-dd");
	static final SimpleDateFormat TimeFormatter = new SimpleDateFormat("HH:mm");
	static final SimpleDateFormat DateTimeFormatter = new SimpleDateFormat("yyyyMMddHHmmss");
	static final SimpleDateFormat DateTimeFormatter2 = new SimpleDateFormat("MM-dd HH:mm");
	static final SimpleDateFormat DateTimeFormatter3 = new SimpleDateFormat("MM-dd");
	static final SimpleDateFormat DATE_TIME_FORMATTER = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	static final SimpleDateFormat DateFormatterYear = new SimpleDateFormat("yyyy");
	public static final class BeginEnd {
		Calendar begin;
		Calendar end;

		public BeginEnd(Calendar begin, Calendar end) {
			this.begin = begin;
			this.end = end;
		}
		

		public BeginEnd(Date begin, Date end) {
			this.begin = Calendar.getInstance();
			this.begin.setTime(begin);
			this.end = Calendar.getInstance();
			this.end.setTime(end);
		}

		public Calendar getBegin() {
			return this.begin;
		}

		public Calendar getEnd() {
			return this.end;
		}

		public Date getBeginDate() {
			return this.begin.getTime();
		}

		public Date getEndDate() {
			return this.end.getTime();
		}

		public long getSpace() {
			return this.end.getTimeInMillis() - this.begin.getTimeInMillis();
		}
	}

	/**
	 * 距离当前天数
	 * 
	 * @param i
	 * @return
	 */
	public static Date addDay(Date date, int i) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		setHourMinSec(calendar, 0, 0, 0);
		calendar.add(Calendar.DAY_OF_YEAR, i);
		return calendar.getTime();
	}

	/**
	 * 距离当前天数
	 * 
	 * @param i
	 * @return
	 */
	public static Date addDayNow(int i) {
		return addDay(now(), i);
	}

	/**
	 * 距离当前的天数，并且减去�?�?
	 * 
	 * @param i
	 * @return
	 */
	public static Date addDayEnd(int i) {
		Calendar calendar = Calendar.getInstance();
		setHourMinSec(calendar, 0, 0, 0);
		calendar.add(Calendar.DAY_OF_YEAR, i);
		calendar.add(Calendar.SECOND, -1);
		return calendar.getTime();
	}

	/**
	 * 毫秒转时�?
	 * 
	 * @param str
	 * @return
	 */
	public static Date longToDate(long str) {
		Date aDate = new Date();
		aDate.setTime(str);
		return null;
	}

	/**
	 * 距离�?段时间的日期
	 * 
	 * @param begin
	 *            多少天前
	 * @param end
	 *            多少天后
	 * @return
	 */
	public static List<Date> getDates(int begin, int end) {
		List<Date> result = new ArrayList<Date>();
		for (int i = begin; i < end; i++) {
			Calendar calendar = Calendar.getInstance();
			calendar.add(Calendar.DAY_OF_YEAR, i);
			result.add(calendar.getTime());
		}
		return result;
	}

	/**
	 * �?个时间段
	 * 
	 * @param begin
	 *            �?始天�?
	 * @param end
	 *            结束天数
	 * @return
	 */
	public static BeginEnd getBetween(int begin, int end) {
		Date b = addDayNow(begin);
		Date e = addDayEnd(end);
		return new BeginEnd(b, e);
	}

	/**
	 * 当天的开始结束时�?
	 * 
	 * @return
	 */
	public static BeginEnd todayBetween() {
		Date b = addDayNow(0);
		Date e = addDayEnd(1);
		return new BeginEnd(b, e);
	}

	/**
	 * 设置时分秒毫�?
	 * 
	 * @param calendar
	 * @param h
	 * @param m
	 * @param s
	 */
	public static void setHourMinSec(Calendar calendar, int h, int m, int s) {
		calendar.set(11, h);
		calendar.set(12, m);
		calendar.set(13, s);
		calendar.set(14, 0);
	}

	/**
	 * 在时间之�?
	 * 
	 * @param now
	 *            标准时间
	 * @param date
	 *            判断时间
	 * @return
	 */
	public static boolean after(Date now, Date date) {
		return now.after(date);
	}

	/**
	 * 在时间之�?
	 * 
	 * @param now
	 *            标准时间
	 * @param date
	 *            判断时间
	 * @return
	 */
	public static boolean before(Date now, Date date) {

		return now.before(date);
	}

	public static boolean between(Date now, Date begin, Date end) {
		return now.getTime() >= begin.getTime() && now.getTime() < end.getTime();
	}

	public static boolean betweenNow(Date begin, Date end) {
		return between(now(), begin, end);
	}

	/**
	 * �������Calendarת������������
	 * 
	 * @param Calendar
	 * @param SimpleDateFormat
	 * @return String
	 */
	public static String parseDateToStr(Calendar cal, SimpleDateFormat formatter) {
		Date time = cal.getTime();
		String timeStr = parseDateToStr(time, formatter);
		return timeStr;
	}
	
	/**
	 * �������Dateת������������
	 * 
	 * @param Date
	 * @param SimpleDateFormat
	 * @return
	 */
	public static String parseDateToStr(Date time, SimpleDateFormat formatter) {
		String timeStr = formatter.format(time);
		return timeStr;
	}

	/**
	 * 
	 * @param Date
	 * @return
	 */
	@SuppressWarnings("unused")
	private static String parseDateToStr(Date time) {
		String timeStr = DateTimeFormatter.format(time);
		return timeStr;
	}

	/**
	 * 
	 * @param String
	 * @return Date
	 */
	public static Date parseStrToDate(String str) {
		Date dateTime = new Date();
		try {
			dateTime = DateTimeFormatter.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return dateTime;
	}
	
	/**
	 * 
	 * @param int begin
	 * @param int end
	 * @return List<Calendar>
	 */
	public static List<Calendar> getDateList(int begin, int end) {
		List<Calendar> result = new ArrayList<Calendar>();
		for (int i = begin; i <= end; i++) {
			Calendar calendar = Calendar.getInstance();
			calendar.add(6, i);
			result.add(calendar);
		}
		return result;
	}
	
	/**
	 * 
	 * @param long
	 * @return Date
	 */
	public static Date parseStrToDate(long longStr) {
		Date aDate = new Date();
		aDate.setTime(longStr);
		return aDate;
	}



	/**
	 * 指定日期字符格式
	 * @param str
	 * @param sdf
	 * @return
	 */
	public static Date parseStrToDate(String str,SimpleDateFormat sdf){
		Date dateTime = new Date();
		try {
			dateTime = sdf.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return dateTime;
	}

	
	/**
	 * 时间差毫�?
	 * 
	 * @param value
	 * @return
	 */
	public static long apart(Date now, Date value) {

		return (now.getTime() - value.getTime());
	}

	public static Date toDateTime(String str) {
		try {
			return DateTimeFormatter.parse(str);
		} catch (ParseException localParseException) {
		}
		return null;
	}

	public static boolean afterNow(Date date) {
		return after(new Date(), date);
	}

	public static boolean beforeNow(Date date) {
		return before(new Date(), date);

	}

	/**
	 * 时间加分�?
	 * 
	 * @param n
	 * @return
	 */
	public static Date addMin(Date date, int n) {
		Calendar aCal = Calendar.getInstance();
		aCal.setTime(date);
		aCal.add(Calendar.MINUTE, n);
		return aCal.getTime();
	}

	/**
	 * 当前时间
	 * 
	 * @return
	 */
	public static Date now() {
		Calendar calendar = Calendar.getInstance();
		return calendar.getTime();
	}

	/**
	 * 获取当前年份
	 * @return
	 */
	public static String getCurrentYear() {
		String timeStr = DateFormatterYear.format(now());
		return timeStr;
	}

	/**
	 * 当前时间加分�?
	 * 
	 * @param n
	 * @return
	 */
	public static Date addNowMin(int n) {
		return addMin(new Date(), n);
	}

	/**
	 * 和当前的时间差毫�?
	 * 
	 * @param value
	 * @return
	 */
	public static long apartNow(Date value) {
		return apart(new Date(), value);
	}

	/**
	 * 格式化日期yyyyMMddHHmmss
	 * 
	 * @param value
	 * @return
	 */
	public static String format(Date value) {
		return DateTimeFormatter.format(value);
	}

	/**
	 * 格式化日期yyyy-mm-dd
	 * 
	 * @param a
	 * @return
	 */
	public static String formatDate(Date a) {
		return DateFormatter.format(a);
	}

	/**
	 * 格式化时间HH:mm
	 * 
	 * @param a
	 * @return
	 */
	public static String formatTime(Date a) {
		return TimeFormatter.format(a);
	}
	
	/**
	 * 格式化日期MM-dd HH:mm
	 * @param time
	 * @param pattern
	 * @return
	 */
	public static String formatDayTime(Date time){
		return DateTimeFormatter2.format(time);
	}
	
	/**
	 * 格式化日期MM-dd
	 * @param time
	 * @return
	 */
	public static String formatDateMMdd(Date time){
		return DateTimeFormatter3.format(time);
	}
	
	/**
	 * 
	 * @param int day
	 * @return Calendar
	 */
	public static Calendar getNowDay(int day) {
		Calendar calendar = Calendar.getInstance();
		setHourMinSec(calendar, 0, 0, 0);
		calendar.add(Calendar.DAY_OF_YEAR, day);
		return calendar;
	}

	
	
	/**
	 * 
	 * @param int begin
	 * @param int end
	 * @return BeginEnd
	 */
	public static BeginEnd getBeginEnd(int begin, int end) {
		Calendar b = getNowDay(begin);
		Calendar e = getNowDay(end);
		return new BeginEnd(b, e);
	}
	
	/**
	 * ����ʱ��
	 * 
	 * @param Date date
	 * @param int h
	 * @param int m
	 * @param int s
	 */
	@SuppressWarnings("deprecation")
	public static void setHourMinSec(Date date, int h, int m, int s) {
		date.setHours(h);
		date.setSeconds(s);
		date.setMinutes(m);
	}


}
